/*
 * SM_WS.c
 *
 *  Created on: 2023年8月24日
 *      Author: zlw
 */
#include "device_type.h"
#ifdef SM_KP
#include "app_api.h"
#include "app_log.h"
#include "app_cli_cmd.h"
#include "bsp_gpio.h"
#include "app_button.h"
#include "app_led.h"
#include "bsp_iadc.h"
#include "em_emu.h"
#include "softwareMultiTimer.h"
#include "device.h"
#include "bsp_system.h"
#include "net_protocol.h"
#include "app_key_value.h"
#include "bsp_rmu.h"
#include "bsp_wdog.h"
#include "RBFVersion.h"
#include "em_i2c.h"
#include "SM_keypad.h"
#include "bsp_nfc.h"
#include "bsp_pca9555.h"
#include "bsp_nfc_lpcd.h"
#include "bsp_timer.h"
#include "app_pwm.h"
#include "app_keyScan.h"
#include "git_version.h"
#include "net_aseq.h"
#include "bsp_i2c.h"
#include "btl_reset_info.h"

#define DEVICE_TX_POWER_DEFAULT  (160)
#define DEVICE_CTUNE_DEFAULT     (125)
#define DEVICE_LFXO_CTUNE_DEFAULT (40)
#define IADC_TIMER_INTERVAL      (240)  //s
#define APP_MAJOR_VERSION        (1)
#define APP_MINOR_VERSION        (1)
#define APP_STAGE_VERSION        (2) //max 255
#define SENSOR_VERSION           ((APP_MAJOR_VERSION << 16)|(APP_MINOR_VERSION<<8)|(APP_STAGE_VERSION))

static void setTxpowerDefaule(void);
static void setCtuneDefaule(void);
static void setLfxoCtuneDefaule(void);
static void gpio_state_change_handler(void);
void SM_alarm_led_start(uint8_t *data);
static void SM_button_init(void);
static void SM_iadc_init(void);
static void printSoftWareInformation(void);
void SM_pca9555_init (void);
void SM_I2C_init (void);

static uint8_t s_option[3] = {0};

/**
 * @brief: 弱点继电器项目相关外设初始化
 * */
void SM_main(void)
{
  setTxpowerDefaule();
  setCtuneDefaule();
  setLfxoCtuneDefaule();
  pwm_init();
  keyscan_driver_init();
  startWatchDogTimer();
  SM_I2C_init();
  SM_pca9555_init();
  SM_button_init();
  SM_iadc_init();
  printSoftWareInformation();
}

/**
 * @brief: 遥控器项目while函数相关处理
 * */
void SM_process(void)
{
  keyValue_t KeyValue = {0x00};
  keyPadValue_t padValue = {0x00};

  feedWatchDogHandler();
  SM_key_value_handler();
  if(key_value_buffer_get(&padValue)) {
      KeyValue.id = 0x31;
      KeyValue.valueSize = padValue.valueCnt+1;
      KeyValue.value[0] = padValue.valueCnt;
      memcpy(&KeyValue.value[1], padValue.value, padValue.valueCnt);
      set_key_value(&KeyValue);
  }
  gpio_state_change_handler();
  pwm_sound_light_stateMachine_run();
  keyPad_led_send();
//  lpcd_irq_event(); //TODO: 后期应用增加
}

/******************get device sleep state***************************/

bool device_sleep_state_get(void)
{
  bool keyScanSleepsState = key_scan_sleep_state_get();
  bool keyScanWakeUpState = key_pad_wake_up_by_mistake_state_get();
  bool pwmSleepState = getPwmIdleState();
  return (keyScanSleepsState && keyScanWakeUpState && pwmSleepState);
}

/**************** set tx power/ctune defaule***********************/
static void setTxpowerDefaule(void)
{
  int16_t txPowerDeciDbm = 0;
  int ret = getThisParam(RfTxPower, &txPowerDeciDbm);
  if(ret != 0) {
      txPowerDeciDbm = DEVICE_TX_POWER_DEFAULT;
      setThisParam(RfTxPower, &txPowerDeciDbm);
  }
  RAIL_Status_t status = bsp_rf_set_tx_power_bdm(txPowerDeciDbm);
  if (status != RAIL_STATUS_NO_ERROR) {
      DS_app_log_error("set tx power error, status = 0x%x\r\n", status);
  }
}

static void setCtuneDefaule(void)
{
  uint32_t ctune = 0;
  int ret = getThisParam(RfCtune, &ctune);
  if(ret != 0) {
      ctune = DEVICE_CTUNE_DEFAULT;
      setThisParam(RfCtune, &ctune);
  }
  RAIL_Status_t status = bsp_rf_set_ctune(ctune);
  if (status != RAIL_STATUS_NO_ERROR) {
      DS_app_log_error("set ctune error, status = 0x%x\r\n", status);
  }
}

static void setLfxoCtuneDefaule(void)
{
  uint8_t ctune = 0;
  int ret = getThisParam(lfxoCtune, &ctune);
  if(ret != 0) {
      ctune = DEVICE_LFXO_CTUNE_DEFAULT;
      setThisParam(lfxoCtune, &ctune);
  }
  CMU_OscillatorTuningSet(cmuOsc_LFXO,ctune);
}

/************************button handler****************************/

static struct Button btn = {0};
static MultiTimer buttonTimer[2] = {
    {.Handle = 0},
    {.Handle = 1},
};

static void ds_btn_state_change_callback(uint8_t intNo, void *ctx);

static ds_button_t button[3] = {
    {
        .port = gpioPortA,
        .pin = 5,
        .mode = gpioModeInput,
        .initial = 1,
        .valid = 0,
        .ds_button_state_change = ds_btn_state_change_callback,
    },
    {
        .port = gpioPortB,
        .pin = 1,
        .mode = gpioModeInput,
        .initial = 0,
        .valid = 0,
        .ds_button_state_change = ds_btn_state_change_callback,
    },
    {
        .port = FM17622_IRQ_PORT,
        .pin = FM17622_IRQ_PIN,
        .mode = gpioModeInputPull,
        .initial = 1,
        .valid = 0,
        .ds_button_state_change = ds_btn_state_change_callback,
    },
};
/**
 * @brief: button timer callback,每10ms轮询一次button状态
 * */
keyValue_t outputKeyValue = {0x00};

static void buttonTimerCallback(MultiTimer* timer, void *userData)
{
  (void)userData;
  uint8_t data[3] = {15,0,1};

  outputKeyValue.value[0] = 0;

  if(timer->Handle == 0) {
      button_handler(&btn);
  }
  else if(timer->Handle == 1) {
      if(read_button_level(&button[1]) == button[1].valid) {
          outputKeyValue.value[1] = 0;
      }
      else {
          outputKeyValue.value[1] = 1;
          SM_alarm_led_start(data);
      }
      set_key_value(&outputKeyValue);
      if(false == device_test_sleep_state_get()) {
          DS_app_log_error("temple = 0x%02x \r\n", outputKeyValue.value[1]);
      }
  }
}

/**
 * @brief:button 1 引脚状态改变时启动button timer
 * */
static void ds_btn_state_change_callback(uint8_t intNo, void *ctx)
{
  (void)intNo;
  ds_button_t *btnDrive = (ds_button_t *)ctx;

  if(btnDrive->port == button[0].port && btnDrive->pin == button[0].pin && buttonTimer[0].status == EN_MULTITIMER_STATUS_IDLE) {
      button_handler(&btn);
  }
  else if(btnDrive->port == button[1].port && btnDrive->pin == button[1].pin && buttonTimer[1].status == EN_MULTITIMER_STATUS_IDLE) {
      softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &buttonTimer[1], 983, buttonTimerCallback,NULL,0);
  }
  else if(btnDrive->port == button[2].port && btnDrive->pin == button[2].pin && read_button_level(&button[2]) == button[2].valid) {
      DS_app_log_info("FM17622 irq ... \r\n");
  }
}

/**
 * @brief: 读取button引脚状态
 * @param： button id
 * @return： gpio 状态
 * */
static uint8_t read_button_GPIO(uint8_t button_id)
{
  // you can share the GPIO read function with multiple Buttons
  switch(button_id)
  {
    case 0:
      return read_button_level(&button[0]);
      break;
    default:
      return 0;
      break;
  }
}

bool isBattVoltageHigh(void)
{
  return true;
}

static void btn_press_handler(void* butt)
{
  Button *btn = (Button *)butt;

  if(btn->button_id == 0) {
      if(btn->event == LONG_PRESS_START) {
          startCommissiongJoin(buttonTrigger, other_device);
      }
      else if(btn->event == SINGLE_CLICK) {
          if(false == device_test_sleep_state_get()) {
              DS_app_log_error("reg btn test \r\n");
          }
      }
  }
}

static void gpio_state_change_handler(void)
{
  if(read_button_level(&button[0]) == button[0].valid) {
      if(buttonTimer[0].status == EN_MULTITIMER_STATUS_IDLE) {
          button_handler(&btn);
      }
  }
  if(read_button_level(&button[1]) != outputKeyValue.value[1]) {
      if(buttonTimer[1].status == EN_MULTITIMER_STATUS_IDLE) {
          softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &buttonTimer[1], 983, buttonTimerCallback,NULL,0);
      }
  }
}

/**
 * @brief: button reed初始化；注册不同状态相关回调函数
 * */
static void SM_button_init(void)
{
  ds_button_init(&button[0]);
  ds_button_init(&button[1]);
  ds_button_init(&button[2]);

  getThisParam(ENpbyOption, s_option);

  uint8_t data[5] = {0, 3, 0, 0, 1};
  SM_led_start(data, POWER_ON_BLINK);

  outputKeyValue.id = 0x10;
  outputKeyValue.valueSize = 2;
  outputKeyValue.value[1] = read_button_level(&button[1]);

  button_init(&btn, &buttonTimer[0], buttonTimerCallback, read_button_GPIO, button[0].valid, 0);
  button_attach(&btn, LONG_PRESS_START,       btn_press_handler);
  button_attach(&btn, SINGLE_CLICK,           btn_press_handler);
}

/************************led init***************************************/

static MultiTimer signalLedTimer;
static keypad_signal_led_t signalLedDriver = {0x00};

static MultiTimer alarmLedTimer;
static keypad_alarm_led_t alarmLedDriver = {0x00};
/**
 * @brief: led闪烁开始
 * @param： led物模型指针；5Bytes：R G Bbitmask bitTime cycleTimes
 * */
void SM_led_start(uint8_t *data, blinkType_t type)
{
  keypad_signal_led_blink_start_init(&signalLedDriver, &signalLedTimer, data, type);
}

void SM_alarm_led_start(uint8_t *data)
{
  keypad_alarm_led_blink_start_init(&alarmLedDriver, &alarmLedTimer, data);
}

/*******************battery detect***************************/
static MultiTimer iadcTimer[2] = {
    {.Handle = 0},
    {.Handle = 1},
};
static uint8_t adcState = 0x00;

static void iadcTimerCallback(MultiTimer* timer, void *userData)
{
  (void)timer;
  (void)userData;

  if(timer->Handle == 0) {
      if(adcState == 0) {
          if(getPwmIdleState() == true) {
              initIADC(iadcPosInputPortCPin0, iadcPosInputPortCPin7);
          }
          softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &iadcTimer[1], 655, iadcTimerCallback,NULL,0);
      }
      else if(adcState == 1) {
          SM_iadc_init();
      }
  }
  else if(timer->Handle == 1) {
      iadc_scan_stop();
      keyPad_led_off(BAT_CTRL);
      adcState = 1;
      softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &iadcTimer[0], 32768*IADC_TIMER_INTERVAL, iadcTimerCallback,NULL,0);
  }
}

/**
 * @brief: adc init
 * */
static void SM_iadc_init(void)
{
  adcState = 0;
  keyPad_led_on(BAT_CTRL);
  softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &iadcTimer[0], 3277, iadcTimerCallback,NULL,0);
}

/**
 * @brief: 获取电压值
 * @return: battery unit:mV
 * */
uint16_t battery = 1500;
uint8_t SM_battery_get(void)
{
  uint16_t bat = 2*get_adc_channel(0);
  if(false == device_test_sleep_state_get()) {
      DS_app_log_error("battery = %d \r\n", bat);
  }
  if(bat >= 600) {
      battery = bat;
  }
  return (battery/10);
}

/************************option key value handler*************************************/

extern bool alarmSoundStateGet(void);

void recv_option_key_value_handler(uint8_t *option)
{
  if(option[0] != s_option[0] || option[1] != s_option[1] || option[2] != s_option[2]) {
      if(((s_option[0]>>7)&0x01) == 0 && ((option[0]>>7)&0x01) == 1) {
          key_scan_sleep_state_set(true);
      }
      setThisParam(ENpbyOption, option);
      memcpy(s_option, option, 3);
  }

  if(key_scan_sleep_state_get()  || ((s_option[0]>>7)&0x01))
    return;

  static uint8_t blinkState = 0;

  if((option[1] & 0x0f) == 0 && (false == alarmSoundStateGet())) {
      pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_OFF, true, true);
  } else if((option[1] & 0x0f) == 1) {
      pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_INTRUSION_ALARM, true, false);
  } else if((option[1] & 0x0f) == 2) {
      pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_FIRE, true, false);
  } else if((option[1] & 0x0f) == 3) {
      pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_SOS, true, false);
  } else if((option[1] & 0x0f) == 4) {
      pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_MEDICAL, true, false);
  }
  if(((option[1] >> 4) & 0x01)) {
      keyPad_led_on(FAULT_ORANGE);
  }
  else {
      keyPad_led_off(FAULT_ORANGE);
  }
  if(((option[1] >> 5) & 0x01)) {
      keyPad_led_on(DEPLOY_BLUE);
  }
  else {
      keyPad_led_off(DEPLOY_BLUE);
  }
  if(((option[1] >> 6) & 0x03) == 0) {
      blinkState = 0;
      uint8_t data[3] = {0,0,255};
      SM_alarm_led_start(data);
  } else if(((option[1] >> 6) & 0x03) == 1) {
      blinkState = 1;
      keyPad_led_on(ALERM_RED);
  } else if(((option[1] >> 6) & 0x03) == 2 && blinkState != 2) {
      blinkState = 2;
      uint8_t data[3] = {15,0,255};
      SM_alarm_led_start(data);
  }
}

/************************operation key value handler*************************************/

void recv_operation_key_value_handler(uint8_t *operation)
{
  if(key_scan_sleep_state_get() || ((s_option[0]>>7)&0x01))
    return;

  if((operation[0] & 0x0f) == 10) {
      pwm_blink_start(SOUND_BLINK_TYPE_OPERATION_TIPS, 2, true, true);
  } else if((operation[0] & 0x0f) == 11) {
      pwm_blink_start(SOUND_BLINK_TYPE_OPERATION_TIPS, 2, true, false);
  } else if((operation[0] & 0x0f) == 12) {
      pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_OFF, true, true);
      pwm_blink_start(SOUND_BLINK_TYPE_OPERATION_TIPS, 2, true, true);
  } else if((operation[0] & 0x0f) == 13) {
      pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_OFF, true, true);
      pwm_blink_start(SOUND_BLINK_TYPE_OPERATION_TIPS, 2, true, false);
  } else if((operation[0] & 0x0f) == 14) {
      pwm_blink_start(SOUND_BLINK_TYPE_OPTION_FAIL, 3, true, true);
  } else if((operation[0] & 0x0f) == 15) {
      pwm_soundLight_start(SOUND_LIGHT_OUT_TYPE_OFF, true, true);
      pwm_blink_start(SOUND_BLINK_TYPE_OPTION_FAIL, 3, true, true);
  }
}

/************************temperature get*************************************/

/**
 * @brief: Get the internal temperature of the chip
 * @return: temperature
 * */
int8_t SM_tempDrv_get(void)
{
  float tempCelsius;
  int8_t result;

  tempCelsius = EMU_TemperatureGet();

  if (tempCelsius < INT8_MIN) {
    tempCelsius = INT8_MIN;
  } else if (tempCelsius > INT8_MAX) {
    tempCelsius = INT8_MAX;
  }

  // adding extra 0.5 before truncating to simulate rounding behavior
  result = (((int8_t) (tempCelsius + 0.5 - INT8_MIN)) + INT8_MIN);

  return result;
}

/*
 * fm17622 gpio init
 * fm17622 IRQ        PA08    IN
 *         RST(NPD)   PC07    OUT
 */
void SM_fm17622_init (void)
{
  fm17622_hard_reset();
  fm17622_get_version();

  DS_app_log_debug("lpcd init ret=%d\n",lpcd_init_reg());//LPCD初始化
  lpcd_set_mode(LPCD_MODE_ENABLE);//返回LPCD模式
#if 0 //TODO:测试代码
  while(1){
      DS_app_log_debug("sleep\n");
      EMU_EnterEM3(false);
      lpcd_irq_event();

  }
#endif

}
/*
 *@brief pca9555 init
 *
 *@note D1~D10    P0_0          =1,led on//Keyad backlight
 *@note D22       P0_5, P0_6    =0,led on
 *@note D24       P0_4          =0,led on
 *@note D12       P0_3          =0,led on
 *@note D13       P0_7          =0,led on
 *@note D14       P1_6, P1_7    =0,led on
 *@note           P1_0          =0, on
 */
void SM_pca9555_init (void)
{
  uint8_t pdata[2];
  BootloaderResetCause_t* resetCause = (BootloaderResetCause_t*) (SRAM_BASE);

  if(resetCause->reason == BOOTLOADER_RESET_REASON_I2C_ERROR){
      DS_app_log_error("reset reason: I2C ERROR\n");
  }

  //set output
  pdata[0] = 0x00;
  pdata[1] = 0x00;
  pca9555_gpio_config(pdata);

  keyPad_led_off(ALL_GPIO);

}
/************************I2C init*************************************/
/*
 *
 */
void SM_I2C_init (void)
{
  // Use default settings
  I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;

  CMU_ClockEnable(cmuClock_I2C0, true);

  // Using PD3 (SDA) and PD2 (SCL)
  GPIO_PinModeSet(I2C_SDA_PORT, I2C_SDA_PIN, gpioModeWiredAndPullUpFilter, 1);
  GPIO_PinModeSet(I2C_SCL_PORT, I2C_SCL_PIN, gpioModeWiredAndPullUpFilter, 1);

  // Route I2C pins to GPIO
  GPIO->I2CROUTE[0].SDAROUTE = (GPIO->I2CROUTE[0].SDAROUTE & ~_GPIO_I2C_SDAROUTE_MASK)
                        | (I2C_SDA_PORT << _GPIO_I2C_SDAROUTE_PORT_SHIFT
                        | (I2C_SDA_PIN << _GPIO_I2C_SDAROUTE_PIN_SHIFT));
  GPIO->I2CROUTE[0].SCLROUTE = (GPIO->I2CROUTE[0].SCLROUTE & ~_GPIO_I2C_SCLROUTE_MASK)
                        | (I2C_SCL_PORT << _GPIO_I2C_SCLROUTE_PORT_SHIFT
                        | (I2C_SCL_PIN << _GPIO_I2C_SCLROUTE_PIN_SHIFT));
  GPIO->I2CROUTE[0].ROUTEEN = GPIO_I2C_ROUTEEN_SDAPEN | GPIO_I2C_ROUTEEN_SCLPEN;

  // Initialize the I2C
  I2C_Init(I2C0, &i2cInit);

  // Set the status flags and index
//  i2c_startTx = false;

  // Enable automatic STOP on NACK
  I2C0->CTRL = I2C_CTRL_AUTOSN;

  //I2C Device gpio init.
  //SM_pca9555_init();
  //SM_fm17622_init();

}
/**********************get hard information***************************/

/**
 * @brief: get hard information
 * @param: point
 * @return: value length
 * */
uint8_t get_hard_information(uint8_t *data)
{
  hardinfo_t hardInfo = {0x00};
  char psn[16] = "DH0080N00001";
  int ret = 0;

  ret = getThisParam(ENpbyKeyM, hardInfo.psn);
  if(ret != 0) {
      memcpy(hardInfo.psn, psn, sizeof(psn));
      setThisParam(ENpbyKeyM, hardInfo.psn);
  }

  ret = getThisParam(ENbyDeviceType, &hardInfo.deviceType);
  if(ret != 0) {
      hardInfo.deviceType = EN_DEVICE_TYPE_LED_KEYPAD;
      setThisParam(ENbyDeviceType, &hardInfo.deviceType);
  }

  ret = getThisParam(ENbyModelType, &hardInfo.modelType);
  if(ret != 0) {
      hardInfo.modelType = 0x01;
      setThisParam(ENbyModelType, &hardInfo.modelType);
  }

  ret = getThisParam(ENbyHardType, &hardInfo.hardType);
  if(ret != 0) {
      hardInfo.hardType = 0x01;
      setThisParam(ENbyHardType, &hardInfo.hardType);
  }

  ret = getThisParam(ENpbyMacID, hardInfo.pmacID);
  if(ret != 0) {
      getMacAddr(hardInfo.pmacID);
      setThisParam(ENpbyMacID, hardInfo.pmacID);
  }

  ret = getThisParam(ENpbyRfVer, hardInfo.pRfVer);
  if(ret != 0
      || hardInfo.pRfVer[0] != MAJOR_VERSION
      || hardInfo.pRfVer[1] != MINOR_VERSION
      || hardInfo.pRfVer[2] != STAGE_VERSION) {
      hardInfo.pRfVer[0] = MAJOR_VERSION;
      hardInfo.pRfVer[1] = MINOR_VERSION;
      hardInfo.pRfVer[2] = STAGE_VERSION;
      setThisParam(ENpbyRfVer, hardInfo.pRfVer);
  }

  ret = getThisParam(ENpbySensorVer, &hardInfo.sensorVer);
  if(ret != 0
      || hardInfo.sensorVer[0] != APP_MAJOR_VERSION
      || hardInfo.sensorVer[1] != APP_MINOR_VERSION
      || hardInfo.sensorVer[2] != APP_STAGE_VERSION) {
      hardInfo.sensorVer[0] = APP_MAJOR_VERSION;
      hardInfo.sensorVer[1] = APP_MINOR_VERSION;
      hardInfo.sensorVer[2] = APP_STAGE_VERSION;
      setThisParam(ENpbySensorVer, &hardInfo.sensorVer);
  }

  memcpy(data, &hardInfo, sizeof(hardInfo));

  return sizeof(hardInfo);
}

/**********************print informatio**************************/
static void printSoftWareInformation(void)
{
  uint8_t macAddr[8] = {0x00};
  getMacAddr(macAddr);
  int16_t txPowerDeciDbm = 0;
  uint32_t ctune = 0;
  char reason[12] = {0};
  uint8_t lfxoTune= 0;
  uint8_t SNNumber[16] = {0x00};

  APP_PRINTF("\r\n*************information***************\r\n");
  uint32_t rst_cause = ds_rmu_init();
  resetToString(rst_cause, reason);
  APP_PRINTF("reset reason   : %s\r\n",reason);
  APP_PRINTF("device mac     : ");
  for(uint8_t i=0;i<7;i++){
      APP_PRINTF("%02x:", macAddr[i]);
  }
  APP_PRINTF("%02x \r\n", macAddr[7]);
  APP_PRINTF("stack version  : v%d.%d.%d \r\n", (STACK_VERSION>>16&0xFF), (STACK_VERSION>>8&0xFF),STACK_VERSION&0xFF);
  APP_PRINTF("app  version   : v%d.%d.%d \r\n", (SENSOR_VERSION>>16&0xFF), (SENSOR_VERSION>>8&0xFF),SENSOR_VERSION&0xFF);
  APP_PRINTF("git  version   : 0x%x\r\n", GIT_VERSION);
  getThisParam(ENpbyKeyM, SNNumber);
  APP_PRINTF("SN number      : %s\r\n", SNNumber);
  APP_PRINTF("device type    : 0x%02X\r\n", EN_DEVICE_TYPE_LED_KEYPAD);
  printfDeviceRfFreqInfo();
  getThisParam(RfTxPower, &txPowerDeciDbm);
  APP_PRINTF("device power   : %d \r\n", txPowerDeciDbm);
  getThisParam(RfCtune, &ctune);
  APP_PRINTF("device ctune   : %ld \r\n", ctune);
  getThisParam(lfxoCtune, &lfxoTune);
  APP_PRINTF("device lfxoTune: %d \r\n", lfxoTune);
  APP_PRINTF("build app time : %s %s \r\n", __DATE__, __TIME__);
#ifdef  CONFIG_JENKINS_BUILD_NUMBER
  APP_PRINTF("jenkins build number : %d\r\n", CONFIG_JENKINS_BUILD_NUMBER);
#else
  APP_PRINTF("jenkins build number : 0\r\n");//没定义默认0
#endif
  APP_PRINTF("***************************************\r\n\r\n");
}

void keyPad_output_keyValue_report(uint8_t state)
{
  if(state == 0x11) {
      outputKeyValue.value[0] = 2;
  } else if(state == 0x12) {
      outputKeyValue.value[0] = 1;
  } else if(state == 0x13) {
      outputKeyValue.value[0] = 4;
  }
  set_key_value(&outputKeyValue);
}

/***********************get model api******************************/

static uint8_t device_input_output_state_get(uint8_t *buffer)
{
  buffer[0] = 0x00;
  if(read_button_level(&button[1]) == button[1].valid)
    buffer[1] = 0x00;
  else
    buffer[1] = 0x01;
  return 2;
}

uint8_t get_device_state(uint8_t *buffer)
{
  uint8_t data[8] = {0x00};
  uint8_t data_size = 0x00;
  uint8_t index = 0;

  buffer[index] = 0x10;
  index += 1;
  data_size = device_input_output_state_get(data);
  memcpy(&buffer[index], data, data_size);
  memset(data, 0, data_size);
  index += data_size;

  return index;
}

static QRFH1_t globalQrfH1 = {0};
static MultiTimer findMeTimer;

static void findMeTimerCallback(MultiTimer* timer, void *userData)
{
  (void)timer;
  (void)userData;
  uint8_t data[5] = {0};
  SM_led_start(data, NONE_BLINK);
}

void SM_device_QRF_state_Handle(QRFH1_t qrfH1, QRFH2_t qrfH2)
{
  uint8_t data[5] = {0};
  if(qrfH2.isFM == 1) {
      data[1] = 15;
      data[3] = 1;
      data[4] = 15;
      SM_led_start(data, FIND_ME_BLINK);
      softwareMultiTimerStop(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &findMeTimer,0);
      softwareMultiTimerStart(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &findMeTimer, 32768*30, findMeTimerCallback,NULL,0);
  }
  else {
      if(findMeTimer.status != EN_MULTITIMER_STATUS_IDLE) {
          softwareMultiTimerStop(EN_LP_HARD_TIMER_EM3_SYS_RTC_1, &findMeTimer,0);
          SM_led_start(data, NONE_BLINK);
      }
  }

  if(qrfH1.IOen != globalQrfH1.IOen) {
      globalQrfH1.IOen = qrfH1.IOen;
  }
}

/**************************option handler******************************/

uint8_t s_option_index_value_get(uint8_t index)
{
  if(index<3)
    return s_option[index];
  else
    return 0;
}

/**********************set gpio mode default*************************/

void staticPowerGpioInit(void)
{
  key_value_buffer_clear();
  CMU_ClockEnable(cmuClock_GPIO, true);
  GPIO_PinModeSet(gpioPortA, 3,gpioModePushPull,1);
  GPIO_PinModeSet(gpioPortA, 4,gpioModeInputPull,0);
  GPIO_PinModeSet(gpioPortB, 0,gpioModeInputPull,1);
  GPIO_PinModeSet(gpioPortC, 0,gpioModeInput,0);
  GPIO_PinModeSet(gpioPortC, 7,gpioModeInput,0);
}

#endif




